package com.ama.controller;

import com.ama.pojo.Employee;
import com.ama.service.EmployeeService;
import com.ama.utils.SpringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class ApplyController {
    @RequestMapping("/apply")
    public void applytoc(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<script>");
        long p_id = Long.parseLong(request.getParameter("p_id"));
        long c_id = Long.parseLong(request.getParameter("c_id"));
        EmployeeService employeeService = SpringUtils.getBean(EmployeeService.class);
        Employee employee = SpringUtils.getBean(EmployeeService.class).query(p_id, c_id);
        if(employee==null){
            SpringUtils.getBean(EmployeeService.class).insert(p_id, c_id);
            out.println("alert('申请成功')");
        }else{
            out.println("alert('您已申请过该公司！不能重复申请！')");
        }
        out.println("window.open ('"+request.getContextPath()+"/index.jsp','_top')");
        out.println("</script>");
        out.println("</html>");
        out.flush();
        out.close();
    }
}
